<template>
  <div>
    <div class="app-container">
      <div class="search-container">
        <el-button type="danger" @click="submitForm(ruleFormRef)">
          ＋ 新增
        </el-button>
        <el-button type="danger" disabled>保存</el-button>
        <el-button type="danger" disabled>撤销</el-button>
        <el-button type="danger" disabled>删除</el-button>
        <h3>基本信息</h3>
        <hr>

        <el-form ref="ruleFormRef" :inline="true" :model="addform" :rules="rules" label-width="auto"
          class="demo-ruleForm" :size="formSize" status-icon>
          <el-form-item label="耳号" prop="earTag">
            <el-select v-model="addform.earTag" placeholder="请选择耳号" style="width: 200px;" clearable>
              <el-option v-for="item in erars" :label="item.earTagNumber" :value="item.earTagNumber" />
            </el-select>
          </el-form-item>
          <el-form-item label="品种" prop="variety">
            <el-select v-model="addform.variety" placeholder="请选择" style="width: 200px;" clearable>
              <el-option label="杜泊" :value="1" />
              <el-option label="澳洲白" :value="2" />
              <el-option label="萨福克" :value="3" />
              <el-option label="杜湖" :value="4" />
              <el-option label="杜杜湖" :value="5" />
              <el-option label="萨杜湖" :value="6" />
              <el-option label="澳湖" :value="7" />
              <el-option label="湖羊" :value="8" />
              <el-option label="澳杜湖" :value="9" />
            </el-select>
          </el-form-item>
          <el-form-item label="电子耳标号">
            <el-input v-model="addform.electEarNum" placeholder="请输入电子耳标号" style="width: 250px;" />
          </el-form-item>
          <el-form-item label="基因等级">
            <el-select v-model="addform.geneticGrade" placeholder="请选择基因等级" style="width: 150px;" clearable>
              <el-option label="后背级" :value="1" />
              <el-option label="核心级" :value="2" />
              <el-option label="生产级" :value="3" />
              <el-option label="未定级" :value="4" />
            </el-select>
          </el-form-item>

          <h3>出生信息</h3>
          <el-form-item label="父亲耳号">
            <el-select v-model="addform.prantEarNum" placeholder="请选择父亲耳号" style="width: 200px;" clearable>
              <el-option v-for="item in fathererars" :label="item.earTag" :value="item.earTag" />
            </el-select> </el-form-item>
          <el-form-item label="母亲耳号">
            <el-select v-model="addform.matherEarNum" placeholder="请选择母亲耳号" style="width: 200px;" clearable>
              <el-option v-for="item in mathererars" :label="item.earTag" :value="item.earTag" />
            </el-select> </el-form-item>
          <el-form-item label="出生日期" required>
            <el-col :span="11">
              <el-form-item prop="date1">
                <el-date-picker v-model="addform.birthDate" type="date" aria-label="Pick a date" format="YYYY-MM-DD"
                  value-format="YYYY-MM-DD" placeholder="Pick a date" style="width: 200px" />
              </el-form-item>
            </el-col>
          </el-form-item>
          <el-form-item label="入场日期" required>
            <el-col :span="11">
              <el-form-item prop="date1">
                <el-date-picker v-model="addform.admissionDate" type="date" aria-label="Pick a date" format="YYYY-MM-DD"
                  value-format="YYYY-MM-DD" placeholder="Pick a date" style="width: 200px" />
              </el-form-item>
            </el-col>
          </el-form-item>
          <el-form-item label="出生场地">
            <el-input v-model="addform.birthPlace" placeholder="请输入出生场地" style="width: 250px;" />
          </el-form-item>
          <el-form-item label="出生重(公斤)">
            <el-input v-model="addform.birthWeight" placeholder="请输入出生重(公斤)" style="width: 250px;" />
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="addform.birthContent" placeholder="请输入备注" type="textarea" style="width: 650px;" />

          </el-form-item>
        </el-form>

      </div>
    </div>
  </div>
</template>
<script setup lang="ts">
import sheepfile from '@/api/ProductionManagement/index'
import { Addsheepfile } from '@/api/ProductionManagement/model'
import purcure from '@/api/PurchaseMent/index'
import { startQuert } from '@/api/EarTagManagement/model'
import type { ComponentSize, FormInstance, FormRules } from 'element-plus'

const formSize = ref<ComponentSize>('default')
const ruleFormRef = ref<FormInstance>()

//参数
const addform = reactive({
  "earTag": "",
  "variety": null,
  "sheepSex": false,
  "electEarNum": "",
  "geneticGrade": null,
  "prantEarNum": "",
  "matherEarNum": "",
  "birthDate": "2024-07-27 10:59:43",
  "birthState": 0,
  "admissionDate": "2024-07-27 10:59:43",
  "birthPlace": "",
  "birthWeight": null,
  "birthContent": "",
  "sheepType": 2
})

//耳号
const erars = ref([])
const getSheepNos = () => {
  purcure.getNoUseSheepNo().then(res => {
    erars.value = res.data
  })
}



//父亲耳号
const fathererars = ref([])
const getSheepfaears = () => {
  const form = reactive({
    presentState: 0,
    sheepType: 1
  })
  sheepfile.updatesheepfilestatus(form.presentState, form.sheepType).then(res => {
    fathererars.value = res.data
  })
}
//母亲耳号
const mathererars = ref([])
const getSheepmaears = () => {
  const form = reactive({
    presentState: 0,
    sheepType: 2
  })
  sheepfile.updatesheepfilestatus(form.presentState, form.sheepType).then(res => {
    mathererars.value = res.data
  })
}

//验证
const rules = reactive<FormRules<RuleForm>>({
  earTag: [
    { required: true, message: '请输入耳号', trigger: 'blur' },
  ],
  variety: [
    { required: true, message: '请选择品种', trigger: 'blur' },
  ],

})


//添加方法
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      sheepfile.addsheepfile(addform)
        .then(res => {
          if (res.data > 0) {
            ElMessage.success('添加成功')
          }
          else if (res.data == -1) {
            ElMessage.error('该品种下不存在该耳号')
          }
          else if (res.data == -2) {
            ElMessage.error('该耳号已被使用')
          }
          else {
            ElMessage.error('添加失败')
          }
        })
    } else {
      console.log('error submit!', fields)
    }
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}

onMounted(() => {
  getSheepNos()
  getSheepfaears()
  getSheepmaears()
})
</script>
